/*
 * 用户注册界面
 */
package com.wonyesoft.wuliu.acticity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.google.zxing.CaptureActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.acticity.utils.StringUtils;
import com.wonyesoft.wuliu.acticity.utils.ToastUtils;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.model.Address;
import com.wonyesoft.wuliu.model.CargoInfo;
import com.wonyesoft.wuliu.model.CargoTransferLog;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;

public class UserSearchListActivity extends BaseActivity implements
		OnClickListener {

	private LinearLayout gonggao, gengxin, about_us, tousu;
	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView title, head_right;
	private ImageView head_left;
	private ImageButton order_erweima, order_button;
	private EditText order_inquiry;
	private static final int GET_CODE = 0;
	private AppContent app;
	private PreferencesCookieStore preferencesCookieStore;
	private ListView listview;
	private HttpUtils http = new HttpUtils();
	public static Gson gson = new Gson();
	private RequestParams params;
	private List<CargoTransferLog> sourceDateList;
	private ListView mylist;
	private SimpleAdapter adapter;
	private List<Map<String, Object>> list;

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acticity_order_inquiry);
		System.out.println("regsiter");
		title = (TextView) findViewById(R.id.head_title);
		order_inquiry = (EditText) findViewById(R.id.order_inquiry);
		order_erweima = (ImageButton) findViewById(R.id.order_erweima);
		order_button = (ImageButton) findViewById(R.id.order_button);
		head_left = (ImageView) findViewById(R.id.head_left);
		head_left.setVisibility(0);
		mylist = (ListView) findViewById(R.id.a0_common);
		title.setText("查询订单");
		head_left.setOnClickListener(this);
		initClick();
	}

	private void initClick() {
		// TODO Auto-generated method stub
		order_erweima.setOnClickListener(this);
		order_button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		case R.id.order_erweima:
			startActivityForResult(new Intent(this, CaptureActivity.class),
					GET_CODE);

			// finish();
			break;
		case R.id.order_button:
			if (StringUtils.isEmpty(order_inquiry.getText().toString()))
				showLongToast("请输入单号");
			searchList();
			break;

		default:
			break;
		}
	}

	private void searchList() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("flowId", order_inquiry.getText().toString() + "");
		System.out.println("flowid:"+ order_inquiry.getText().toString());
		JsonRequest jr = JsonRequest.getInit();
		jr.setData(dataMap);
		String json = gson.toJson(jr);
		params = new RequestParams();
		params.addBodyParameter("jsonParam", json);
		//http.configCookieStore(AppContent.getPreferencesCookieStore());
		http.send(HttpRequest.HttpMethod.POST, GagApi.SEARCHlIST, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {

						System.out.println("进入地址查询");
						Gson gson2 = new Gson();
						JsonResponse jrs = gson2.fromJson(responseInfo.result,
								JsonResponse.class);
						System.out.println(responseInfo.result);
						State state = jrs.getState();

						System.out.println(preferencesCookieStore);
						if (state.getCode().equals(State.SUCEECCODE)) {
							// 存用户登录信息
							System.out.println(responseInfo.result);

							if (gson2 != null) {
								Type type = new TypeToken<List<CargoTransferLog>>() {
								}.getType();
								try {
									sourceDateList = gson2.fromJson(
											new JSONObject(responseInfo.result)
													.getJSONObject("data")
													.getJSONArray("rows")
													+ "", type);
								} catch (JsonSyntaxException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if (null != sourceDateList)
									initViews();
							}

						} else
							System.out.println(state.getMsg());
						// ToastUtils.showLong(state.getMsg());

					}

					@Override
					public void onFailure(HttpException error, String msg) {

						ToastUtils.showLong("查询失败");
					}
				});
	}

	Bundle bundle = new Bundle();
	protected void initViews() {
		mylist = (ListView) findViewById(R.id.a0_common);
		mylist.setDividerHeight(0);

		adapter = new SimpleAdapter(this, getData(),
				R.layout.activity_searchmyorder_conent,
				new String[] { "myorder_content_qitext",
						"myorder_content_zhongtext" }, new int[] {
						R.id.myorder_content_qitext,
						R.id.myorder_content_zhongtext });

		mylist.setAdapter(adapter);
		mylist.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				System.out.println("点了那个坐标:" + position);
				HashMap<String, Object> map = (HashMap<String, Object>) mylist
						.getItemAtPosition(position);
				System.out.println("nv" + map.get("driverId")
						+ "");
				
				  bundle.putString("driverId", map.get("driverId") + ""); openActivity(LbsDriversActivity.class, bundle);
				}

		});

	}

	private List<Map<String, Object>> getData() {
		list = new ArrayList<Map<String, Object>>();

		for (CargoTransferLog goods : sourceDateList) {

			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("myorder_content_qitext", goods.getTransferContent());
			// map.put("myorder_content_time", "2015 b0903");
			map.put("myorder_content_zhongtext", goods.getCreateTime());
			map.put("driverId", goods.getDriverId());
			// System.out.println(goods.getCreateTime());
			list.add(map);
		}

		return list;
	}

	// 扫描查单返回
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == 0) {
			String result = data.getExtras().getString("result");
			if (TextUtils.isEmpty(result)) {
				Toast.makeText(this, "uucc", Toast.LENGTH_LONG).show();
				return;
			}

			order_inquiry.setText(result);

		}
	}

}
